package com.oglvip.business.remote.support.request;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * 请求详细信息
 */
public class RequestEntity {

    private int httpMethod = GET;

    private String host;

    private String parentPath;

    private String path;

    private String contentType;

    private String accept;

    /**
     * post对象时使用
     */
    private Object body;
    private Map<String, Object> formParams = new HashMap<String, Object>();
    private Map<String, Object> urlParams = new HashMap<String, Object>();

    public RequestEntity(String host, String parentPath, String path) {
        this.host = host;
        this.path = path;
        this.parentPath = parentPath;
    }

    /**
     * @return URL String
     */
    public String toUrl() {
        return buildPath();
    }

    public String buildParam() {
        if (urlParams.isEmpty()) {
            return "";
        }
        StringBuilder builder = new StringBuilder("?");
        for (Iterator<Map.Entry<String, Object>> it = urlParams.entrySet().iterator(); it.hasNext(); ) {
            Map.Entry<String, Object> entry = it.next();
            if (entry.getValue() != null) {
                builder.append(entry.getKey() + "=" + entry.getValue());
            }
            if (it.hasNext()) {
                builder.append("&");
            }
        }
        return builder.toString();
    }

    /**
     * buildPath
     */
    private String buildPath() {

        if (parentPath.endsWith("/")) {
            parentPath = parentPath.substring(parentPath.length(), parentPath.length() - 1);
        }

        if (path.endsWith("/")) {
            path = path.substring(0, path.length() - 1);
        }

        String url = host + "" + parentPath + "" + path + "" + buildParam();

        return url;
    }

    public static final int GET = 0;
    public static final int POST = 1;
    public static final int DELETE = 2;
    public static final int PUT = 3;

    public int getHttpMethod() {
        return httpMethod;
    }

    public void setHttpMethod(int httpMethod) {
        this.httpMethod = httpMethod;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getParentPath() {
        return parentPath;
    }

    public void setParentPath(String parentPath) {
        this.parentPath = parentPath;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }

    public Object getBody() {
        return body;
    }

    public void setBody(Object body) {
        this.body = body;
    }

    public Map<String, Object> getFormParams() {
        return formParams;
    }

    public void setFormParams(Map<String, Object> formParams) {
        this.formParams = formParams;
    }

    public void setUrlParams(Map<String, Object> urlParams) {
        this.urlParams = urlParams;
    }

    public String getAccept() {
        return accept;
    }

    public void setAccept(String accept) {
        this.accept = accept;
    }
}
